2020-2021 Sunseeker Telemetry and Lighting System
eusci_b_i2c_ex1_masterRxMultiple.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 //*****************************************************************************
33 //
53 //
54 //*****************************************************************************
55 
56 #include "driverlib.h"
57 
58 //*****************************************************************************
59 //
60 //Set the address for slave module. This is a 7-bit address sent in the
61 //following format:
62 //[A6:A5:A4:A3:A2:A1:A0:RS]
63 //
64 //A zero in the "RS" position of the first byte means that the master
65 //transmits (sends) data to the selected slave, and a one in this position
66 //means that the master receives data from the slave.
67 //
68 //*****************************************************************************
69 #define SLAVE_ADDRESS 0x48
70 //*****************************************************************************
71 //
72 //Specify Expected Receive data count.
73 //
74 //*****************************************************************************
75 #define RXCOUNT 0x05
76 
77 
78 //******************************************************************************
79 // MSP430FR57xx Demo - USCI_B0 I2C Master RX multiple bytes from MSP430 Slave
80 //
81 // Description: This demo connects two MSP430's via the I2C bus. The master
82 // reads 5 bytes from the slave. This is the MASTER CODE. The data from the slave
83 // transmitter begins at 0 and increments with each transfer.
84 // The USCI_B0 RX interrupt is used to know when new data has been received.
85 // ACLK = n/a, MCLK = SMCLK = BRCLK = DCO = 1MHz
86 //
87 // /|\ /|\
88 // MSP430FR5739 10k 10k MSP430F5739
89 // slave | | master
90 // ----------------- | | -----------------
91 // -|XIN P1.6/UCB0SDA|<-|----+->|P1.6/UCB0SDA XIN|-
92 // | | | | | 32kHz
93 // -|XOUT | | | XOUT|-
94 // | P1.7/UCB0SCL|<-+------>|P1.7/UCB0SCL |
95 // | | | P1.0|--> LED
96 //
97 //******************************************************************************
98 uint8_t RXData;
99 void main (void)
100 {
101  WDT_A_hold(WDT_A_BASE);
102 
103  //Set DCO frequency to 8MHz
104  CS_setDCOFreq(CS_DCORSEL_0,CS_DCOFSEL_3);
105  //Set ACLK = DCO with frequency divider of 8
106  CS_initClockSignal(CS_ACLK,CS_DCOCLK_SELECT,CS_CLOCK_DIVIDER_8);
107  //Set SMCLK = DCO with frequency divider of 8
108  CS_initClockSignal(CS_SMCLK,CS_DCOCLK_SELECT,CS_CLOCK_DIVIDER_8);
109  //Set MCLK = DCO with frequency divider of 8
110  CS_initClockSignal(CS_MCLK,CS_DCOCLK_SELECT,CS_CLOCK_DIVIDER_8);
111 
112  // Configure Pins for I2C
113  //Set P1.6 and P1.7 as Secondary Module Function Input.
114  /*
115 
116  * Select Port 1
117  * Set Pin 6, 7 to input Secondary Module Function, (UCB0SIMO/UCB0SDA, UCB0SOMI/UCB0SCL).
118  */
119  GPIO_setAsPeripheralModuleFunctionInputPin(
120  GPIO_PORT_P1,
121  GPIO_PIN6 + GPIO_PIN7,
122  GPIO_SECONDARY_MODULE_FUNCTION
123  );
124 
125  //Init I2C master
126  EUSCI_B_I2C_initMasterParam param = {0};
127  param.selectClockSource = EUSCI_B_I2C_CLOCKSOURCE_SMCLK;
128  param.i2cClk = CS_getSMCLK();
129  param.dataRate = EUSCI_B_I2C_SET_DATA_RATE_400KBPS;
130  param.byteCounterThreshold = RXCOUNT;
131  param.autoSTOPGeneration = EUSCI_B_I2C_SEND_STOP_AUTOMATICALLY_ON_BYTECOUNT_THRESHOLD;
132  EUSCI_B_I2C_initMaster(EUSCI_B0_BASE, &param);
133 
134  //Specify slave address
135  EUSCI_B_I2C_setSlaveAddress(EUSCI_B0_BASE,
137  );
138 
139  //Set Master in receive mode
140  EUSCI_B_I2C_setMode(EUSCI_B0_BASE,
141  EUSCI_B_I2C_RECEIVE_MODE
142  );
143 
144  //Enable I2C Module to start operations
145  EUSCI_B_I2C_enable(EUSCI_B0_BASE);
146 
147  EUSCI_B_I2C_clearInterrupt(EUSCI_B0_BASE,
148  EUSCI_B_I2C_RECEIVE_INTERRUPT0 +
149  EUSCI_B_I2C_BYTE_COUNTER_INTERRUPT +
150  EUSCI_B_I2C_NAK_INTERRUPT
151  );
152 
153  //Enable master Receive interrupt
154  EUSCI_B_I2C_enableInterrupt(EUSCI_B0_BASE,
155  EUSCI_B_I2C_RECEIVE_INTERRUPT0 +
156  EUSCI_B_I2C_BYTE_COUNTER_INTERRUPT +
157  EUSCI_B_I2C_NAK_INTERRUPT
158  );
159 
160  //Set P1.0 as an output pin.
161  GPIO_setAsOutputPin(
162  GPIO_PORT_P1,
163  GPIO_PIN0
164  );
165 
166  while (1)
167  {
168  __delay_cycles(2000);
169 
170  while (EUSCI_B_I2C_SENDING_STOP ==
171  EUSCI_B_I2C_masterIsStopSent(EUSCI_B0_BASE));
172 
173  EUSCI_B_I2C_masterReceiveStart(EUSCI_B0_BASE);
174 
175  __bis_SR_register(CPUOFF+GIE); // Enter LPM0 w/ interrupt
176  }
177 }
178 
179 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
180 #pragma vector=USCI_B0_VECTOR
181 __interrupt
182 #elif defined(__GNUC__)
183 __attribute__((interrupt(USCI_B0_VECTOR)))
184 #endif
185 void USCIB0_ISR(void)
186 {
187  static uint8_t count = 0;
188  switch(__even_in_range(UCB0IV,0x1E))
189  {
190  case 0x00: break; // Vector 0: No interrupts break;
191  case 0x02: break; // Vector 2: ALIFG break;
192  case 0x04:
193  EUSCI_B_I2C_masterReceiveStart(EUSCI_B0_BASE);
194  break; // Vector 4: NACKIFG break;
195  case 0x06: break; // Vector 6: STT IFG break;
196  case 0x08: break; // Vector 8: STPIFG break;
197  case 0x0a: break; // Vector 10: RXIFG3 break;
198  case 0x0c: break; // Vector 14: TXIFG3 break;
199  case 0x0e: break; // Vector 16: RXIFG2 break;
200  case 0x10: break; // Vector 18: TXIFG2 break;
201  case 0x12: break; // Vector 20: RXIFG1 break;
202  case 0x14: break; // Vector 22: TXIFG1 break;
203  case 0x16:
204  RXData = EUSCI_B_I2C_masterReceiveSingle(
205  EUSCI_B0_BASE
206  ); // Get RX data
207  if (++count >= RXCOUNT) {
208  count = 0;
209  __bic_SR_register_on_exit(CPUOFF); // Exit LPM0
210  }
211  break; // Vector 24: RXIFG0 break;
212  case 0x18: break; // Vector 26: TXIFG0 break;
213  case 0x1a:
214  GPIO_toggleOutputOnPin(
215  GPIO_PORT_P1,
216  GPIO_PIN0
217  );
218  break; // Vector 28: BCNTIFG break;
219  case 0x1c: break; // Vector 30: clock low timeout break;
220  case 0x1e: break; // Vector 32: 9th bit break;
221  default: break;
222  }
223 }
224 
void USCIB0_ISR(void)
MPU_initThreeSegmentsParam param
__bic_SR_register_on_exit(LPM3_bits|GIE)
__delay_cycles(500000)